package com.wechat.back.controller;

import com.wechat.back.anno.OptLog;
import com.wechat.back.pojo.comment.ArticleComment;
import com.wechat.back.pojo.comment.PoiComment;
import com.wechat.back.pojo.tools.PageBean;
import com.wechat.back.pojo.tools.Result;
import com.wechat.back.service.CommentService;
import com.wechat.back.service.SensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wechat.back.utils.SensitiveWordRead.readSensitiveWords;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @OptLog(optModule = "评论模块",optType = "添加", optDesc = "添加文章评论")
    @PostMapping("/addArticleComment")
    public Result addArticleComment(@RequestBody @Validated(ArticleComment.Add.class) ArticleComment articleComment) {
        List<String> SensitiveWords = readSensitiveWords();
        sensitiveWordService.loadSensitiveWord(SensitiveWords);
        if (sensitiveWordService.hasSensitiveWord(articleComment.getCommentContent())) {
            //包含敏感词
            articleComment.setStatue("未通过");
            return Result.success("包含敏感词");
        }else {
            articleComment.setStatue("已通过");
            commentService.addArticleComment(articleComment);
            return Result.success();
        }
    }

    @OptLog(optModule = "评论模块",optType = "添加", optDesc = "添加poi评论")
    @PostMapping("/addPoiComment")
    public Result addPoiComment(@RequestBody @Validated(PoiComment.Add.class) PoiComment poiComment) {
        List<String> SensitiveWords = readSensitiveWords();
        sensitiveWordService.loadSensitiveWord(SensitiveWords);
        if (sensitiveWordService.hasSensitiveWord(poiComment.getCommentContent())) {
            //包含敏感词
            poiComment.setStatue("未通过");
            return Result.success("包含敏感词");
        }else {
            poiComment.setStatue("已通过");
            commentService.addPoiComment(poiComment);
            return Result.success();
        }
    }

    @GetMapping("/listArticleComment")
    public Result<PageBean<ArticleComment>> listArticleComment(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer commentId,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer articleId,
            @RequestParam(required = false) String username
    ){
        PageBean<ArticleComment> pb = commentService.listArticleComment(pageNum,pageSize,username,commentId,userId,articleId);
        return Result.success(pb);
    }

    @GetMapping("/listPoiComment")
    public Result<PageBean<PoiComment>> listPoiComment(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer commentId,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer poiId,
            @RequestParam(required = false) String username
    ){
        PageBean<PoiComment> pb = commentService.listPoiComment(pageNum,pageSize,username,commentId,userId,poiId);
        return Result.success(pb);
    }

    @OptLog(optModule = "评论模块",optType = "删除", optDesc = "删除文章评论")
    @DeleteMapping("/deleteArticleComment")
    public Result deleteArticleComment(Integer commentId) {
        commentService.deleteArticleComment(commentId);
        return Result.success();
    }

    @OptLog(optModule = "评论模块",optType = "删除", optDesc = "删除poi评论")
    @DeleteMapping("/deletePoiComment")
    public Result deletePoiComment(Integer commentId) {
        commentService.deletePoiComment(commentId);
        return Result.success();
    }

    @OptLog(optModule = "评论模块",optType = "修改", optDesc = "修改文章评论")
    @PutMapping("/updateArticleComment")
    public Result updateArticleComment(@RequestBody @Validated(ArticleComment.Update.class) ArticleComment articleComment) {
        commentService.updateArticleComment(articleComment);
        return Result.success();
    }

    @OptLog(optModule = "评论模块",optType = "修改", optDesc = "修改poi评论")
    @PutMapping("/updatePoiComment")
    public Result updatePoiComment(@RequestBody @Validated(PoiComment.Update.class) PoiComment poiComment) {
        commentService.updatePoiComment(poiComment);
        return Result.success();
    }

    @OptLog(optModule = "评论模块",optType = "更新", optDesc = "修改文章点赞数")
    @PutMapping("/updateArticleLikeCount")
    public Result updateArticleLikeCount(Integer likeCount, Integer commentId) {
        commentService.updateArticleLikeCount(likeCount,commentId);
        return Result.success();
    }

    @OptLog(optModule = "评论模块",optType = "更新", optDesc = "修改poi点赞数")
    @PutMapping("/updatePoiLikeCount")
    public Result updatePoiLikeCount(Integer likeCount, Integer commentId) {
        commentService.updatePoiLikeCount(likeCount,commentId);
        return Result.success();
    }
}
